/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.objects.texture;

import java.nio.IntBuffer;

import org.easyway.utils.Utility;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.EXTFramebufferObject;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

public class TextureFBO {
	int id;

	IntBuffer bufferId;

	Texture texture;

	TextureCompact texturec;

	public TextureFBO(TextureCompact destination) {
		texturec = destination;
		// if (texture. <= 0)
		// return;

		// check
		if (!checkFBOSupport())
			return;

		// generate the ID
		bufferId = BufferUtils.createIntBuffer(1);
		EXTFramebufferObject.glGenFramebuffersEXT(bufferId);
		id = bufferId.get();

		EXTFramebufferObject.glBindFramebufferEXT(
				EXTFramebufferObject.GL_FRAMEBUFFER_EXT, id);
		EXTFramebufferObject.glFramebufferTexture2DEXT(
				EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
				EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
				GL11.GL_TEXTURE_2D, texturec.texture, 0);
	}

	public TextureFBO(Texture destination) {
		texture = destination;
		// if (texture. <= 0)
		// return;

		// check
		if (!checkFBOSupport())
			return;

		// generate the ID
		bufferId = BufferUtils.createIntBuffer(1);
		EXTFramebufferObject.glGenFramebuffersEXT(bufferId);
		id = bufferId.get();

		EXTFramebufferObject.glBindFramebufferEXT(
				EXTFramebufferObject.GL_FRAMEBUFFER_EXT, id);
		EXTFramebufferObject.glFramebufferTexture2DEXT(
				EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
				EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
				GL11.GL_TEXTURE_2D, texture.dataid.id, 0);
	}

	public static final boolean checkFBOSupport() {
		boolean FBOSupported = GLContext.getCapabilities().GL_EXT_framebuffer_object;
		if (!FBOSupported) {
			Utility.error("Your graphic card doesn't support the FBO",
					"StripesOut()");
			return false;
		}
		return true;
	}

	public void startDrawing() {
		EXTFramebufferObject.glBindFramebufferEXT(
				EXTFramebufferObject.GL_FRAMEBUFFER_EXT, id);
		GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
//		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		if (texture != null)
			GL11.glViewport(0, 0, texture.width, texture.height);
		else
			GL11.glViewport(0, 0, texturec.width, texturec.height);
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		GL11.glLoadIdentity();
	}

	public void endDrawing() {
		GL11.glPopAttrib();
		EXTFramebufferObject.glBindFramebufferEXT(
				EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
	}

}
